package qst.com.bacK.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import qst.com.bacK.utils.RequestUtil;
import qst.com.bacK.utils.WebUtil;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.pojo.entity.AssessmentType;
import qst.com.bacK.pojo.entity.PersonalityDimension;
import qst.com.bacK.service.AssessmentService;
import qst.com.bacK.service.DimensionService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Author:   msh
 * Date:     2024/10/11 下午4:26
 * Description:
 */

@Controller
@RequestMapping("/dimension")
public class DimensionController {
    @Autowired
    private AssessmentService assessmentService;
    @Autowired
    private DimensionService dimensionService;

    @RequestMapping("/list")
    public void listDimension(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String submitButton = RequestUtil.getString(req, "create");
        if (!submitButton.isEmpty()) {
            int assessmentId = RequestUtil.getInt(req, "id");
            AssessmentType assessment = assessmentService.findAssessmentById(assessmentId);
            req.setAttribute("assessment", assessment);
            WebUtil.forward(req, resp, "/dimension/create.jsp");
        } else {
            req.setAttribute("assessmentList", assessmentService.findAllAssessment());
            // 如果是点击左边导航栏进来，则没有ID，所以就不显示知识点列表
            // 如果是点击查看知识点按钮进来的，就有科目ID，所以就显示该科目下的知识点列表
            int id = RequestUtil.getInt(req, "id");
            // if (id > 0) {
            List<PersonalityDimension> dimensionList = dimensionService
                    .findDimensionByAssessment(id);
            req.setAttribute("dimensionList", dimensionList);
            // }
            WebUtil.forward(req, resp, "/dimension/list.jsp");
        }
    }

    // 删除
    @RequestMapping("/delete")
    public void deleteDimension(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");
        try {
            dimensionService.deleteDimension(id);
            req.getSession().setAttribute("_message_key", "知识点已删除");
        } catch (ExamException ex) {
            ex.printStackTrace();
            req.getSession().setAttribute("_error_key", ex.getMessage());
        }
        resp.sendRedirect("/dimension/list.action?id=" + RequestUtil.getInt(req, "assessmentId"));
    }

    // 编辑
    @RequestMapping("/edit")
    public void editDimension(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");
        PersonalityDimension dimension = dimensionService.findDimensionById(id);
        req.setAttribute("dimension", dimension);
        WebUtil.forward(req, resp, "/dimension/edit.jsp");
    }

    // 保存
    @RequestMapping("/save")
    public void saveDimension(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PersonalityDimension kp = new PersonalityDimension();
        kp.setTitle(RequestUtil.getString(req, "title"));
        kp.setDepict(RequestUtil.getString(req, "depict"));
        kp.setAssessmentId(RequestUtil.getInt(req, "assessmentId"));
        try {
            dimensionService.saveDimension(kp);
            req.getSession().setAttribute("_message_key", "知识点信息已保存到数据库");
            resp.sendRedirect("/dimension/view.action?id=" + kp.getId());
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
            req.setAttribute("dimension", kp);
            req.setAttribute("assessment", assessmentService.findAssessmentById(kp.getAssessmentId()));
            WebUtil.forward(req, resp, "/dimension/create.jsp");
        }
    }

    // 更新
    @RequestMapping("/update")
    public void updateDimension(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PersonalityDimension kp = new PersonalityDimension();
        kp.setId(RequestUtil.getInt(req, "id"));
        kp.setTitle(RequestUtil.getString(req, "title"));
        kp.setDepict(RequestUtil.getString(req, "depict"));
        kp.setAssessmentId(RequestUtil.getInt(req, "assessmentId"));
        try {
            dimensionService.updateDimension(kp);
            req.getSession().setAttribute("_message_key", "知识点信息已保存到数据库");
            resp.sendRedirect("/dimension/view.action?id=" + kp.getId());
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
            req.setAttribute("dimension", kp);
            req.setAttribute("assessment", assessmentService.findAssessmentById(kp.getAssessmentId()));
            WebUtil.forward(req, resp, "/dimension/edit.jsp");
        }
    }

    // view
    @RequestMapping("/view")
    public void viewDimension(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");
        PersonalityDimension dimension = dimensionService.findDimensionById(id);
        req.setAttribute("dimension", dimension);
        WebUtil.forward(req, resp, "/dimension/view.jsp");
    }
}